package fun.yao.common.queue;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 循环自动扩展队列
 * modify by csl 2008.11.24 内部使用1.5 JDK的 队列ConcurrentLinkedQueue实现
 */
public final class CycleQueue<T>
{
    /*
    private Lock lock = new ReentrantLock();

    private int EXP_SIZE = 5;
    private int size ;
    private int addPosi = 0 ;
    private int getPosi = 0;
    private Object[] elementData ;
    */
    private final ConcurrentLinkedQueue<T> queue = new ConcurrentLinkedQueue<T>();

    public CycleQueue()
    {
        this(20,5);
    }

    public CycleQueue(int initSize)
    {
        this(initSize,5);
    }

    public CycleQueue(int initSize, int ExpSize)
    {
    	/*
    	size = initSize;
    	EXP_SIZE = ExpSize;
    	elementData = new Object[size];
    	*/
    }

    /**
     * 添加数据对象到队列尾部
     * @param obj 数据对象
     */
    public boolean add(T obj)
    {
        return queue.add(obj);
    	/*
        lock.lock();
        try{
        	if ( addPosi >= size ){
                if ( elementData[0] == null ){
                    elementData[0] = obj;
                    addPosi = 0;
                }else{
                    Object[] oldData = elementData;
                    size = size + EXP_SIZE;
                    elementData = new Object[size];
                    System.arraycopy(oldData,0,elementData,EXP_SIZE,oldData.length);//将新扩展的空间拼接到队列头部
                    elementData[0] = obj;
                    getPosi = EXP_SIZE;
                    addPosi = 0;
                }
            }else{
                if (  elementData[addPosi] != null  ){
                    Object[] old1 = new Object[addPosi];
                    Object[] old2 = new Object[size - addPosi];
                    System.arraycopy(elementData,0,old1,0,old1.length);
                    System.arraycopy(elementData,getPosi,old2,0,old2.length);
                    size = size + EXP_SIZE;
                    getPosi = getPosi + EXP_SIZE;
                    elementData = new Object[size];
                    System.arraycopy(old1,0,elementData,0,old1.length);
                    System.arraycopy(old2,0,elementData,getPosi,old2.length);
                }
                elementData[addPosi] = obj;
            }
            addPosi ++;
        }finally{
        	lock.unlock();
        }
        */
    }

    /**
     * 获取并删除队列头的数据对象
     * @return 返回对队头的数据对象，如果没有，则为null
     */
    public T get()
    {
        return queue.poll();
    	/*
    	T obj = null;
        lock.lock();
        try
        {
        	if ( getPosi >= size ){
	            getPosi = 0;
            }
            obj = (T)elementData[getPosi];
            if ( obj != null ){
		        elementData[getPosi] = null;
		        getPosi ++ ;
            }
        }finally{
        	lock.unlock();
        }
        return obj;
        */
    }

    /**
     * 检索，但是不移除此队列的头，如果此队列为空，则返回 null。
     */
    public T peek()
    {
        return queue.peek();
    }

    /**
     * 获取对队中当前包括数据对象的个数
     * @return
     */
    public int count()
    {
        return queue.size();
    }

    public ConcurrentLinkedQueue<T> getQueue()
    {
        return this.queue;
    }
}